(function(){
	var jAjax = function(input) {
		var reqObj = preProcess(input);
		reqObj.request = new XMLHttpRequest();
		//reqObj.request.onreadystatechange = state_Change;
		reqObj.request.onreadystatechange = function(){
			return state_Change.apply(reqObj, [reqObj]);
		};
		reqObj.request.open(reqObj.method, reqObj.url, true);
		if (reqObj.method === 'POST') {
			reqObj.request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			reqObj.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			reqObj.request.setRequestHeader("Connection", "close");
		}
		reqObj.request.send(reqObj.data);
	};
	function preProcess(input) {
		var reqObj = {data:null, dataType:'*/*'};
		merge(reqObj, input);
		reqObj.method = reqObj.method ? reqObj.method.toUpperCase() : 'GET';
	}
	function merge(obj1, obj2) {
		if (typeof obj2 === 'undefined') {
			obj2 = obj1;
			obj1 = this;
		}
		var i = obj1.length, j = 0;
		if (typeof obj2.length === 'number') {
			for (var l=obj2.length; j < l; j++) {
				obj1[i++] = obj2[j];
			}
		} else {
			while (typeof obj2[j] !== 'undefined') {
				obj[i++] = obj2[j++];
			}
		}
		obj1.length = i;
		return obj1;
	}
	function state_Change(reqObj) {
		if (reqObj.request.readyState === 4) {
			if (reqObj.request.status === 200 || reqObj.request.status === 304) {// 200: Success, 304: NotModified
				// success callback function
				if (reqObj.success) {
					reqObj.success(reqObj.request.responseText, reqObj.request.statusText, reqObj.request);
				}
			} else {
				// error callback function
				if (reqObj.error) {
					reqObj.error(reqObj.request, reqObj.request.statusText);
				}
			}
			// complete callback function
			if (reqObj.complete) {
				reqObj.complete(reqObj.request, reqObj.request.statusText);
			}
		}
	}
	window.jAjax = jAjax;
})(window);